package com.mmw.crm.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.mmw.crm.bo.CustomerPageBo;
import com.mmw.crm.entity.AdminFieldSort;
import com.mmw.crm.entity.AdminScene;
import com.mmw.crm.entity.Customer;
import com.mmw.crm.result.Result;
import com.mmw.crm.service.AdminFieldService;
import com.mmw.crm.service.AdminFieldSortService;
import com.mmw.crm.service.AdminSceneService;
import com.mmw.crm.service.CustomerService;
import com.mmw.crm.vo.AdminSceneVo;
import com.mmw.crm.vo.CustomerSaveEntityVo;
import com.mmw.crm.vo.CustomerSaveVo;
import com.mmw.crm.vo.CustomerVo;
import org.springframework.beans.BeanUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
public class CustomerController {

    @Resource
    CustomerService customerService;

    @Resource
    AdminFieldSortService adminFieldSortService;

    @Resource
    AdminSceneService adminSceneService;

    @Resource
    AdminFieldService adminFieldService;

    @PostMapping("CrmCustomer/queryPageList")
    public Result queryPageList(@RequestBody CustomerVo customerVo) {
        Integer page = customerVo.getPage();
        Integer limit = customerVo.getLimit();
        String search = customerVo.getSearch();
        LambdaQueryWrapper<Customer> wrapper = new LambdaQueryWrapper<>();
        if (!StringUtils.isEmpty(search)) {
            wrapper.like(Customer::getCustomerName, search)
                    .or().like(Customer::getTelephone, search)
                    .or().like(Customer::getMobile,search);
        }
        Page<Customer> pageInfo = new Page<>(page,limit);
        Page<Customer> pageCustomer = customerService.page(pageInfo, wrapper);
        List<Customer> customerList = pageCustomer.getRecords();
        CustomerPageBo customerPageBo = new CustomerPageBo();
        customerPageBo.setTotalRow(customerList.size());
        customerPageBo.setList(customerList);

        return Result.build(customerPageBo,null,0);

    }

    @PostMapping("/field/queryListHead")
    public Result queryListHead(@RequestParam Integer label){
        LambdaQueryWrapper<AdminFieldSort> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AdminFieldSort::getLabel,label);
        List<AdminFieldSort> list = adminFieldSortService.list(wrapper);
        return Result.build(list,null,0);
    }

    @PostMapping("/scene/queryScene")
    public Result queryScene(@RequestParam Integer type){

        LambdaQueryWrapper<AdminScene> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(AdminScene::getType,type);
        List<AdminScene> adminSceneList = adminSceneService.list(wrapper);
        List<AdminSceneVo> adminSceneVo = new ArrayList<>();
        for (AdminScene adminScene:
                adminSceneList) {
            AdminSceneVo adminSceneVoTemp = new AdminSceneVo();
            BeanUtils.copyProperties(adminScene,adminSceneVoTemp);
            adminSceneVoTemp.setIsDefault(0);
            adminSceneVo.add(adminSceneVoTemp);
        }
    return Result.build(adminSceneVo,null,0);

    }

    @PostMapping("/field/queryField")
    public String fieldQueryField(@RequestParam Integer label){
       String result = adminFieldService.queryField(label);
        return result;
    }

    @PostMapping("/CrmCustomer/deleteByIds")
    public Result crmLeadsDeleteByIds(@RequestParam List<Integer> customerIds){
        System.err.println(customerIds.toString());

        customerService.removeByIds(customerIds);
        return Result.build(null,null,0);

    }
    @PostMapping("/CrmCustomer/queryBusiness")
    public String crmCustomerQueryBusiness(@RequestParam Integer customerId){
       String res = customerService.crmCustomerQueryBusiness(customerId);
       return res;
    }

    @PostMapping("/CrmCustomer/addOrUpdate")
    public Result fieldQueryField(@RequestBody CustomerSaveVo customerSaveVo){
        System.err.println("===");

        CustomerSaveEntityVo entityVo = customerSaveVo.getEntity();
        Customer customer = new Customer();
        customer.setCustomerName(entityVo.getCustomer_name());
        customer.setAddress(entityVo.getAddress());
        customer.setMobile(entityVo.getMobile());
        customer.setNextTime(entityVo.getNext_time());
        customer.setRemark(entityVo.getRemark());
        customer.setTelephone(entityVo.getTelephone());
        customer.setWebsite(entityVo.getWebsite());
        customer.setCreateTime(new Date());
        customer.setBatchId("1");
        customer.setDealStatus(1);
        customer.setIsLock(0);
        System.err.println("entityVo"+customer);
        customerService.save(customer);


        return Result.build(customer,null,0);
    }

    @PostMapping("/field/verify")
    public Result fieldQueryField(@RequestParam Integer fieldId,@RequestParam String value){

        return Result.build(null,null,0);
    }




}
